import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { NewCardSetComponent } from '../new-card-set/new-card-set.component';
import { UserListEditRemarkComponent } from '../edit-remark/edit-remark.component';
import { logOutComponent } from '../logOut/log-out.component';
import { CardRessiueComponent } from '../card-ressiue/card-ressiue.component';
import { STData } from '@delon/abc';

@Component({
  selector: 'app-card-manage',
  templateUrl: './card-manage.component.html',
  styleUrls: ["./card-manage.css"]
})
export class CardManageComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  @Input()
  set param(value){
    if(!value) return;
    console.log(value);
    this._param.userName=value.infoName;
    this._param.id=value.id;
    this._param.personType=value.personType;
    this._param.IdNumber=value.papersNumber;
    this.roomId=value.roomId;
  }
  showBtn:boolean=false;
  freeze:string = "冻结";
  selectData: any[];
  loading = false;
  selectedData:any={};
  flag:Boolean=false;//是否办过卡
  roomId:string;
  _param:any={
    userName:"",
    IdNumber:"",
    personType:"",
    id:"",
  };
  program:any={
    "monthlyRent":"",
    "itemType":"",
    "openType":"",
    "itemId":""
  }

  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '编号',
        type: ColumnType.radio,
      },
      {
        index: 'cardId',
        title: '卡号',
      },
      {
        index: 'cardNo',
        title: '虚拟卡号',
        width:150
      },
      {
        index: 'cardCode',
        title: '卡编号',
      },
      {
        index: 'cardType',
        title: '卡类型',
        type: ColumnType.tag,
        tag: {
          1:{text:'IC卡'},
          2:{text:'CPU卡'},
          3:{text:'身份证'},
          4:{text:'居住证'},
          5:{text:'银行卡'},
          6:{text:'APP'},
        },
      },
      {
        index:'cardStatus',
        title:'状态',
        type: ColumnType.tag,
        tag:{
          0:{text:'未激活',color: 'red'},
          1:{text:'已激活', color: 'green'},
          2:{text:'丢失', color: 'red'},
          3:{text:'损坏', color: 'red'},
          4:{text:'冻结', color: 'red'},
          5:{text:'待缴费', color: 'red'},
          6:{text:'已过期', color: 'red'},
          7:{text:'退卡', color: 'red'},
          9:{text:'其他'}
        },
      },
      {
        index: 'infoName',
        title: '持卡人'
      },
      {
        index: 'infoMobile',
        title: '持卡人手机号'
      },
      {
        index:'houseName',
        title:'详细地址',
        width:400,
      },
      {
        index:'endTime',
        title:'到期时间',
      },
      {
        index:'cardRemark',
        title:'注销时间',
      },
      {
        index:'mark',
        title:'备注'
      },
    ],
    showCheckBox: true,
    data:[]
    //reqUrl:'/core/door/card/page',
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService) { }

  //删除卡号后面的8个0
  delCardZero(num:string):string{
    if(num.length > 8){
      num = num.substring(0,num.length - 8);
    }
    return num;
  }

  //新办
  newSet(type:any,userName:any):void{
    const id=this._param.id;
    const program=this.program;
    const flag=this.flag;
    const roomId=this.roomId;
    const modal = this.modal.create({
      nzTitle: '新办门禁卡',
      nzContent: NewCardSetComponent,
      nzWidth: '90%',
      nzComponentParams: {
        type,
        userName,
        id,
        program,
        flag,
        roomId
      },
      nzOnOk: () => {
        this.st.reload();
        this.getInfo();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
        this.getInfo();
      }
    })
  }
  //补办
  makeUp(userName:any):void{
    let arr=Object.keys(this.selectedData);
    if(arr.length == 0){
      this.message.error("请选择卡片!");
      return;
    }
    let doorCard=this.selectedData.cardId;
    let xnCardNo=this.selectedData.cardNo
    let xuDate=this.selectedData.endTime;
    let startDate=this.selectedData.startTime;
    const program=this.program;
    const id=this._param.id;
    const roomId=this.roomId;
   
    const modal = this.modal.create({
      nzTitle: '补办门禁卡',
      nzContent: CardRessiueComponent,
      nzWidth: '90%',
      nzComponentParams: {
        doorCard,
        userName,
        id,
        program,
        xuDate,
        startDate,
        roomId,
        xnCardNo
      },
      nzOnOk: () => {
        this.st.reload();
        this.getInfo();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
        this.getInfo();
      }
    })

  }
  //续期
  renewal(type:any,userName:any):void{
    let arr=Object.keys(this.selectedData);
    if(arr.length == 0){
      this.message.error("请选择卡片!");
      return;
    }
    let doorCard=this.selectedData.cardId;;
    let xnCardNo=this.selectedData.cardNo;
    let xuDate=this.selectedData.endTime;
    let cardStatus = this.selectedData.cardStatus
    let startDate=this.selectedData.startTime;
    let cardCode=this.selectedData.cardCode;
    const program=this.program;
    const id=this._param.id;
    const roomId=this.roomId;
    const modal = this.modal.create({
      nzTitle: '续期门禁卡',
      nzContent: NewCardSetComponent,
      nzWidth: '90%',
      nzComponentParams: {
        type,
        userName,
        doorCard,
        id,
        program,
        xuDate,
        cardStatus,
        startDate,
        cardCode,
        roomId,
        xnCardNo
      },
      nzOnOk: () => {
        this.st.reload();
        this.getInfo();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
        this.getInfo();
      }
    })
  }
  //修改备注
  editRemark():void{
    let arr=Object.keys(this.selectedData);
    if(arr.length == 0){
      this.message.error("请选择卡片!");
      return;
    }
    let _param=this.selectedData;
    const modal = this.modal.create({
      nzTitle: '修改备注',
      nzContent: UserListEditRemarkComponent,
      nzWidth: '40%',
      nzComponentParams: {
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  //注销
  logOut(userName:any):void{
    let arr=Object.keys(this.selectedData);
    if(arr.length == 0){
      this.message.error("请选择卡片!");
      return;
    }
    let doorCard=this.selectedData.cardId;
    let xnCardNo=this.selectedData.cardNo;
    let xuDate=this.selectedData.endTime;
    let startDate=this.selectedData.startTime;
    const program=this.program;
    const id=this._param.id;
   
    const modal = this.modal.create({
      nzTitle: '注销门禁卡',
      nzContent: logOutComponent,
      nzWidth: '90%',
      nzComponentParams: {
        userName,
        doorCard,
        id,
        program,
        xuDate,
        startDate,
        xnCardNo
      },
      nzOnOk: () => {
        this.st.reload();
        this.getInfo();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
        this.getInfo();
      }
    })
  }

  //监听单选按钮事件
  _console(ret):void{
    console.log('change',ret);
    this.selectedData=ret;
    if(ret.length == 0  || ret == undefined){
      this.selectedData={};
      return;
    }
    if(ret!.cardStatus == "4"){//冻结
      this.freeze = "解冻";
    }else{
      this.freeze = "冻结"
    }
  }
  //冻结
  setFreeze():void{
    const self=this;
    let arr=Object.keys(this.selectedData);
    if(arr.length == 0){
      this.message.error("请选择卡片!");
      return;
    }
    let status=this.selectedData.cardStatus;
    let url= status == 4 ? this.api.doorCard.unFreezeCard.url : this.api.doorCard.freezeCard.url;
    this.modal.confirm({
      nzTitle: `确定 ${self.freeze} ?`,
      nzOnOk: () => {
        this.http.post(url,{card:self.selectedData.cardNo}).subscribe((res:any) => {
          console.log(res);
          if(res.status === '200'){
            self.message.success('操作成功');
            self.st.reload();
            self.getInfo();
          }else{
            self.message.error(res.message);
          }
        })
      }
    })
  }



  //根据人员id查询卡信息和人员信息
  getInfo(){
    this.http.post(this.api.doorCard.queryInfo.url,{"personId":this._param.id}).subscribe((res)=>{
      console.log(res);
      if(res.status == 200){
        if(res.data.cardList.length > 0){
          this.flag=true;
        }else{
          this.flag=false;
        }
        this.showBtn=true;
        this.config.data=res.data.cardList;
        this.program.monthlyRent=res.data.payItemVo.monthlyRent;
        this.program.itemType=res.data.payItemVo.itemType;
        this.program.openType=res.data.payItemVo.openType;
        this.program.itemId=res.data.payItemVo.id;
        
      }
    })
  }

  ngOnInit() {
    this.getInfo();
  }

}
